package com.example.admin.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.entity.SysConfig;
import com.example.admin.service.ISysConfigService;
import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.page.Pager;
import com.example.util.page.SeachParamsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统参数表 前端控制器
 * </p>
 *
 * @author wangkai
 * @since 2019-06-04
 */
@RestController
@Api(value = "全局配置Controller", tags = {"全局配置操作接口"})
@RequestMapping("/sys/sysConfig")
public class SysConfigController {

    @Autowired
    private ISysConfigService service;

    /**
     * 获取配置
     *
     * @param pager
     * @return
     */
    @ApiOperation(value = "全局配置列表", httpMethod = "POST")
    @PostMapping(value = "/page")
    public AjaxResult page(@RequestBody Pager pager) {
        Page<SysConfig> page = new Page<>();
        page.setCurrent(pager.getPageNum());
        page.setSize(pager.getPageSize());
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        SeachParamsUtil.loadSearchParam(pager, queryWrapper);
        pager.convertPage(service.page(page, queryWrapper));
        return new AjaxResult(APIState.S00, pager);
    }


    @ApiOperation(value = "全局配置列表", httpMethod = "GET")
    @GetMapping(value = "/list")
    public AjaxResult list() {
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        return new AjaxResult(APIState.S00, service.list(queryWrapper));
    }

    /**
     * 保存配置
     *
     * @param config
     * @return
     */
    @ApiOperation(value = "全局配置新增", httpMethod = "POST")
    @PostMapping
    public AjaxResult save(@RequestBody SysConfig config) {
        if (config != null) {
            if (config.insert()) {
                return new AjaxResult(APIState.S00, "保存成功");
            }
        }
        return new AjaxResult(APIState.S01, "保存失败");
    }

    /**
     * 修改配置
     *
     * @param config
     * @return
     */
    @ApiOperation(value = "全局配置编辑", httpMethod = "PUT")
    @PutMapping
    public AjaxResult update(@RequestBody SysConfig config) {
        if (config != null && config.getId() != null) {
            if (config.insertOrUpdate()) {
                return new AjaxResult(APIState.S00, "保存成功");
            }
        }
        return new AjaxResult(APIState.S01, "保存失败");
    }

    /**
     * 删除配置
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "全局配置删除", httpMethod = "DELETE")
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        if (id != null) {
            SysConfig cf = service.getById(id);
            if (cf != null) {
                if (cf.deleteById()) {
                    return new AjaxResult(APIState.S00, "删除成功");
                }
            } else {
                return new AjaxResult(APIState.S01, "当前配置不存在请重新刷新列表");
            }
        }
        return new AjaxResult(APIState.S01, "删除失败");
    }

}
